#!/usr/bin/env sh

FILE="$(dirname "$0")/.env"

if [ -f "$FILE" ]; then
    echo "$FILE environment file exists."
    echo "Please remove it before generating a new one or use existing one."
    exit 1
fi

POSTGRES_PASSWORD="$(head -c 12 /dev/random  | base64 | tr -d =+/)"
CLOVER_SESSION_SECRET="$(head -c 64 /dev/random | base64)"
CLOVER_COLUMN_ENCRYPTION_KEY="$(head -c 32 /dev/random | base64)"

cat <<EOT > $(dirname "$0")/.env
RACK_ENV=development
POSTGRES_DB=clover
POSTGRES_PASSWORD="$POSTGRES_PASSWORD"
CLOVER_DATABASE_URL="postgres://postgres/clover?user=clover&password=${POSTGRES_PASSWORD}"
CLOVER_SESSION_SECRET="$CLOVER_SESSION_SECRET"
CLOVER_COLUMN_ENCRYPTION_KEY="$CLOVER_COLUMN_ENCRYPTION_KEY"
EOT
